package com.kantboot.functional.im.listener;

import com.alibaba.fastjson2.JSON;
import com.kantboot.functional.im.dto.FunctionalImPushMessageDTO;
import com.kantboot.functional.im.event.WebsocketPushEvent;
import com.kantboot.functional.im.server.WebsocketServer;
import jakarta.annotation.Resource;
import jakarta.websocket.Session;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class WebsocketPushListener {

    @Resource
    private WebsocketServer websocketServer;

    @EventListener
    public void onApplicationEvent(WebsocketPushEvent event) {
        FunctionalImPushMessageDTO message = event.getMessage();
        Session session = event.getSession();
        websocketServer.sendMessageBySession(session, JSON.toJSONString(message));
    }
}
